package com.zzyl.nursing.controller.member;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.MemberReservationDto;
import com.zzyl.nursing.service.MemberReservationService;
import com.zzyl.nursing.vo.Result;
import com.zzyl.nursing.vo.TimestampCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 预约信息Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags = "预约信息相关接口")
@Slf4j
public class MemberReservationController extends BaseController {

    @Autowired
    private MemberReservationService memberReservationService;

/*    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public R<Integer> getCancelledReservationCount() {
        Long userId = UserThreadLocal.getUserId();
        System.out.println("------" + userId);
        return R.ok(1);
    }*/

    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public R<Long> getCancelledReservationCount() {
        Long count = memberReservationService.getCancelledReservationCount();
        return R.ok(count);
    }

    /**
     * 查询每个时间段剩余预约次数
     * @param time
     * @return
     */
    @GetMapping("/countByTime")
    @ApiOperation("查询每个时间段剩余预约次数")
    public TableDataInfo<List<TimestampCountVo>> countByTime(long time) {
        log.info("time的值为 {}", time);
        List<TimestampCountVo> list = memberReservationService.countByTime(time);
        return getDataTable(list);
    }

    /**
     * 新增预约信息
     * @param memberReservationDto
     * @return
     */
    @PostMapping
    public AjaxResult save(@RequestBody MemberReservationDto memberReservationDto) {
        log.info("新增预约信息：{}", memberReservationDto);
        boolean result = memberReservationService.saveReservation(memberReservationDto);
        return result ? success("操作成功") : error("操作失败");
    }


    /**
     * 分页查询预约
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping("/page")
    public Result list(Integer pageNum, Integer pageSize, @RequestParam(defaultValue = "0") Integer status) {
        startPage();
        List<Reservation> list = memberReservationService.selectMemberReservationList(status);
        TableDataInfo dataTable = getDataTable(list);
        return Result.success(dataTable);
    }

    /**
     * 取消预约
     * @param id
     * @return
     */
    @PutMapping("/{id}/cancel")
    public AjaxResult cancelReservation(@PathVariable Long id) {
        log.info("取消预约的ID参数为：{}", id);
        boolean result = memberReservationService.cancelReservation(id);
        return result ? success("取消成功") : error("取消失败");
    }
}